# read in data
library(stringr)
library(data.table)
library(ggplot2)
library(dplyr)
library(lubridate)
library(lfe)
library(sjPlot)
library(stargazer)
library(standardize)
library(broom)
library(fixest)

###################### set your wd
setwd()
################# Load Tweets and FB

congressFBlocal <- readRDS("~Data/congressfb_aggregated_RR.rds")
congresstweetslocal <- readRDS("~Data/congresstweets_aggregated_RR.rds")

#######

###### election result
congresstweetslocal$competitive <- ifelse(congresstweetslocal$PVIABS < 10, 1, 0)
congresstweetslocal$congressparty <- paste0(as.character(congresstweetslocal$congress.x),"-", as.character(congresstweetslocal$party))

congressFBlocal$competitive <- ifelse(congressFBlocal$PVIABS < 10, 1, 0)
congressFBlocal$congressparty <- paste0(as.character(congressFBlocal$congress.x),"-", as.character(congressFBlocal$party))

####### models
congresstweetslocal$congressparty <- paste0(as.character(congresstweetslocal$congress.x),"-", as.character(congresstweetslocal$party))
congressFBlocal$congressparty <- paste0(as.character(congressFBlocal$congress.x),"-", as.character(congressFBlocal$party))


tweetsfe <- felm(Vote_Pct_2PT ~ scale(local) + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y + state, data = congresstweetslocal)
FBfe <- felm(Vote_Pct_2PT ~ scale(local) + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + Cmte_Chair + Qual_Challenger| congressparty + icpsr.y + state, data = congressFBlocal)
tweetspool <- felm(Vote_Pct_2PT ~ scale(local) + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + female + chamber.x + Cmte_Chair + Qual_Challenger| congressparty + state, data = congresstweetslocal)
FBpool <- felm(Vote_Pct_2PT ~ scale(local) + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + female + chamber.x + Cmte_Chair + Qual_Challenger| congressparty + state, data = congressFBlocal)

tab_model(tweetsfe, FBfe,tweetspool, FBpool)
stargazer(tweetsfe, FBfe,tweetspool, FBpool, star.cutoffs = c(.05, .01, .001), type = "html", out = "localelectionresults RR.htm")

##################### interaction model appendix
tweetsfe <- lm(Vote_Pct_2PT ~ local + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + Cmte_Chair + Qual_Challenger + local*PVIParty + congressparty + icpsr.y + state, data = congresstweetslocal)
FBfe <- lm(Vote_Pct_2PT ~ local + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + Cmte_Chair + Qual_Challenger + local*PVIParty + congressparty + icpsr.y + state, data = congressFBlocal)
tweetspool <- lm(Vote_Pct_2PT ~ local + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + female + chamber.x + Cmte_Chair + Qual_Challenger + local*PVIParty + congressparty + state, data = congresstweetslocal)
FBpool <- lm(Vote_Pct_2PT ~ local + NPDistScale + Chamber.Majority + PVIParty + Freshman + Seniority + female + chamber.x + Cmte_Chair + Qual_Challenger + local*PVIParty + congressparty + state, data = congressFBlocal)



P1 <- plot_model(tweetsfe, type = "pred", terms = c("local", "PVIParty [0,10, 30]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Two-Party Vote Share", 
    color = "Party PVI Value",  # Change legend title
    title = "Localized Twitter Pct. Pooled Model"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P2 <- plot_model(FBfe, type = "pred", terms = c("local", "PVIParty [0,10, 30]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Two-Party Vote Share", 
    color = "Party PVI Value",  # Change legend title
    title = "Localized Facebook Pct. Pooled Model"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P3 <-plot_model(tweetspool, type = "pred", terms = c("local", "PVIParty [0,10, 30]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Two-Party Vote Share", 
    color = "Party PVI Value",  # Change legend title
    title = "Localized Twitter Pct. Member FE Model"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

P4 <- plot_model(FBpool, type = "pred", terms = c("local", "PVIParty [0,10, 30]")) +
  labs(
    x = "Local Message Pct.", 
    y = "Two-Party Vote Share", 
    color = "Party PVI Value",  # Change legend title
    title = "Localized Facebook Pct. Member FE Model"  # Add title
  ) +
  theme_bw() +  # Apply the theme
  theme(legend.position = "bottom")  # Move legend to the bottom

library(gridExtra)
grid.arrange(P1, P2, P3, P4, ncol = 2, nrow = 2)
ggsave("Election_Interactions_appendix.png")




###################### plot coefficients

coefs <- read.csv("~/Data/LocalElectionCoefs.csv")

elections <- filter(coefs, Question == "Votes")

ggplot(elections, aes(x=Question, y=coef, group=Measure, color=Measure)) + 
  geom_point(position = position_dodge(.3), size = 2.5) +
  geom_errorbar(aes(ymin = lower, ymax = upper),
                position = position_dodge(.3),
                width = 0) +
  geom_hline(yintercept=0, linetype="dashed", color = "grey50")+
  ylab("Change in Two-Party Vote Share")+
  scale_colour_manual(values = c("grey70", "grey40"))+
  theme_bw() +
  facet_wrap(~Model)+
  theme(
    legend.position = "bottom",
    axis.title.x = element_blank())
ggsave("Elections Local RR.png")